<template>
<div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="naver">
        <el-form  ref="requestParameters" :model="data" >
          <div class="filter-container">
            <el-form-item label="学科名称">
              <el-input
                v-model="data.subjectName "
                @keyup.enter="handleFilter"
                style="width: 200px;margin-right:15px"
                class="filter-item"
              ></el-input>
              <el-button class="filter-item" size="small"  @click="resetForm">清除</el-button>
              <el-button
                class="filter-item"
                size="small"
                type="primary"
                @click="handleFilter"
              >搜索</el-button>
              <el-button
              class="filter-item fr"
              size="small"
              style="margin-left: 10px;"
              @click="handleCreate"
              type="success"
              icon="el-icon-edit"
            >新增学科</el-button>
            </el-form-item>

          </div>
        </el-form>
        <el-alert :title="title" show-icon type="info" :closable="false" class="alert"></el-alert>
        <!-- table数据 -->
        <el-table :cell-style="{ 'text-align': 'center'}" :data="list" size="mini" style="width:100%" highlight-current-row :header-cell-style="{'background':'#fafafa','text-align': 'center'}">
          <el-table-column label="序号" width="50px">
            <template #default="{$index}">
              {{ $index + 1}}
            </template>
          </el-table-column>
          <el-table-column label="学科名称" prop="subjectName"></el-table-column>
          <el-table-column label="创建者" prop="username"></el-table-column>
          <el-table-column label="创建日期" prop="addDate" width="200px"></el-table-column>
          <el-table-column label="前台是否提示" prop="isFrontDisplay">
            <template #default="{row}">
              {{row.isFrontDisplay?'是': '否'}}
            </template>
          </el-table-column>
          <el-table-column label="二级目录" prop="twoLevelDirectory"></el-table-column>
          <el-table-column label="标签" prop="tags"></el-table-column>
          <el-table-column label="题目数量" prop="totals"></el-table-column>
          <el-table-column label="操作"  min-width="150">
           <template #default="{row}">
             <el-button type="text" size="mini" style="margin-right:15px" @click="claaify(row.id,row.subjectName)">学科分类</el-button>
            <el-button type="text" size="mini" style="margin-right:15px" @click="subLabel(row.id,row.subjectName)">学科标签</el-button>
            <el-button type="text" size="mini" style="margin-right:15px" @click="edit(row.id)">修改</el-button>
            <el-button type="text" size="mini" @click="del(row.id)">删除</el-button>
           </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <el-row style="margin-top:25px">
          <el-pagination
          class="fr"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="data.page"
          :page-sizes="[5, 10, 20, 30]"
          :page-size="data.pagesize"
          :total="counts"
          layout=" prev, pager, next,sizes, jumper"
          >
        </el-pagination>
        </el-row>
      </el-card>
    </div>
    <SubjectsAdd ref="subjectadd"></SubjectsAdd>
</div>
</template>

<script>
import { list, remove } from '../../api/hmmm/subjects'
import SubjectsAdd from '../components/subjects-add'
export default {
  components: {
    SubjectsAdd
  },
  data () {
    return {
      data: {
        page: 1,
        pagesize: 10,
        subjectName: ''
      },
      list: [],
      counts: 0,
      showdialog: false

    }
  },
  computed: {
    title () {
      return `数据一共${this.counts}条`
    }
  },
  created () {
    this.getList()
  },
  methods: {
    async getList () {
      const res = await list(this.data)
      console.log(res.data)
      this.counts = res.data.counts
      this.list = res.data.items
      this.list.forEach(ele => { ele.addDate = this.transTime(ele.addDate) })
    },
    transTime (isoDate) {
      if (!isoDate) return '-'
      const date = new Date(isoDate)
      return date.toLocaleString().replace(/\//g, '-')
    },
    resetForm () {
      this.data.subjectName = ''
    },
    handleSizeChange (val) {
      this.data.pagesize = val
      this.page = 1
      this.getList()
    },
    handleCurrentChange (val) {
      this.data.page = val
      this.getList()
    },
    handleFilter () {
      this.getList()
    },
    handleCreate () {
      this.showdialog = true
    },
    edit (id) {
      this.$refs.subjectadd.edit(id)
      this.handleCreate()
    },
    async del (id) {
      try {
        await this.$confirm('此操作将永久删除用户 ' + ', 是否继续?', '提示', {
          type: 'warning'
        })
        await remove({ id })
        this.$message.success('删除成功')
        this.getList()
      } catch {}
    },
    claaify (id, name) {
      this.$router.push('/subjects/directorys' + `?id=${id}&name=${name}`)
    },
    subLabel (id, name) {
      this.$router.push('/subjects/tags' + `?id=${id}&name=${name}`)
    }
  }

}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.alert {
  margin: 10px 0px;
}
.pagination {
  margin-top: 10px;
  // text-align: right;
}
</style>

<style>
.el-table th {
  background-color: #fafafa;
}
.el-table th.is-leaf {
  border-bottom: 2px solid #e8e8e8;
}
/* .el-form-item {
  margin-bottom: 0;
} */
.disabled td {
  background-color: #f9f9f9;
  color: #c1c1c1;
}
.disabled .el-button--primary,
.disabled .el-button--danger {
  background-color: #dbdada;
  border: 1px solid #dbdada;
  color: #999;
  cursor: not-allowed;
}
</style>
